﻿using UnityEngine;
using System.Collections;

enum EnemyAiState { Idle, Chasing,Moving, Hit };

public class LizardManScript : MonoBehaviour {

    [SerializeField]
    private PlayerAnimationController mAnimationManager;

	private float mActionTimer = 0;
	private float mActionDelay = 0.5f;

    private EnemyAiState mAiState = EnemyAiState.Idle;

    private Vector2 TargetPos = Vector2.zero;
    private Vector2 OrigPos = Vector2.zero;
    private Vector2 CurrentPosition = Vector2.zero;
    private int mDistance = 5;
    private int mMaxRange = 30;
    private Vector2 StartPosition;
    private int MonsterSpeed = 3;

    private float KnockBackForce = 0;
    private float KnockBackReduce = 50;
    private Vector2 KnockBackDirection = Vector2.zero;

    void Start()
    {
        StartPosition = this.transform.position;
        CurrentPosition = this.transform.position;
    }

	// Update is called once per frame
	void Update () {
        switch (mAiState)
        {
            case EnemyAiState.Idle:
                HandleIdleState();
                break;
            case EnemyAiState.Moving:
                HandleMoveState();
                break;
            case EnemyAiState.Hit:
                HandleKnockback();
                break;
        }
	}

    private void HandleKnockback()
    {
        CurrentPosition += (KnockBackDirection * KnockBackForce) * Time.deltaTime;
        KnockBackForce -= KnockBackReduce * Time.deltaTime;
        this.transform.position = CurrentPosition;

        if (KnockBackForce <= 0)
        {
            mAiState = EnemyAiState.Idle;
        }
    }

    private void HandleIdleState()
    {
        mActionTimer += Time.deltaTime;

        if (mActionTimer > mActionDelay)
        {
           GameObject player = GetPlayerInRange();
            if (player != null)
            {
                
                Vector2 distance = (new Vector2(player.transform.position.x,player.transform.position.y) - CurrentPosition); 

                Vector3 direction = distance.normalized;
                if (direction.x < 0)
                {
                    mAnimationManager.PlayAnimation(AnimationConst.ANIMATION_WALK, AnimationConst.WEST);
                } if (direction.x >= 0)
                {
                    mAnimationManager.PlayAnimation(AnimationConst.ANIMATION_WALK, AnimationConst.EAST);           
                }
                TargetPos = CurrentPosition + distance;

                CurrentPosition += ((TargetPos - CurrentPosition).normalized * MonsterSpeed) * Time.deltaTime;
                this.transform.position = CurrentPosition;
                
			} else {
                
            int rnd = Mathf.FloorToInt(UnityEngine.Random.value * 4);
            mActionTimer = 0;

            switch (rnd)
            {
                case 0:
                    TargetPos = new Vector2(mDistance, 0) + new Vector2(CurrentPosition.x, CurrentPosition.y);
                    mAnimationManager.PlayAnimation(AnimationConst.ANIMATION_WALK, AnimationConst.EAST);
                    break;
                case 1:
                    TargetPos = new Vector2(0, mDistance) + new Vector2(CurrentPosition.x, CurrentPosition.y);
                    mAnimationManager.PlayAnimation(AnimationConst.ANIMATION_WALK, AnimationConst.NORTH);
                    break;
                case 2:
                    TargetPos = new Vector2(-mDistance, 0) + new Vector2(CurrentPosition.x, CurrentPosition.y);
                    mAnimationManager.PlayAnimation(AnimationConst.ANIMATION_WALK, AnimationConst.WEST);
                    break;
                case 3:
                    TargetPos = new Vector2(0, -mDistance) + new Vector2(CurrentPosition.x, CurrentPosition.y);
                    mAnimationManager.PlayAnimation(AnimationConst.ANIMATION_WALK, AnimationConst.SOUTH);
                    break;
            }

            OrigPos = this.transform.position;

            mAiState = EnemyAiState.Moving;
			}
        }
    }

    private GameObject GetPlayerInRange()
    {
        Collider2D[] colliders = Physics2D.OverlapCircleAll(this.transform.position, 10);
        for (int i = 0; i < colliders.Length; i++)
        {
            if (colliders[i].gameObject.tag == "Player")
            {
                return colliders[i].gameObject;
            }
        }
        return null;
    }

    private void HandleMoveState()
    {
        if (Vector2.Distance(CurrentPosition, OrigPos) >= Vector2.Distance(OrigPos, TargetPos) || CheckIfWithinRange())
        {
            mAiState = EnemyAiState.Idle;
        } else {
            CurrentPosition += ((TargetPos - CurrentPosition).normalized * MonsterSpeed) * Time.deltaTime;
            this.transform.position = CurrentPosition;
        }
    }

    private bool CheckIfWithinRange()
    {
        if (TargetPos.x > StartPosition.x + mMaxRange || TargetPos.x < StartPosition.x - mMaxRange ||
            TargetPos.y > StartPosition.y + mMaxRange || TargetPos.y < StartPosition.y - mMaxRange)
        {
            return true;
        }
        return false;
    }

    public void KnockBack(Vector2 Direction)
    {
        mAiState = EnemyAiState.Hit;
        KnockBackForce = 20;
        KnockBackDirection = Direction;

       // mAnimationManager.PlayAnimation(AnimationConst.WEST, "WestHit");
    }
}
